package citylife.geotools.demo;

import java.net.URL;
import java.util.List;

import javax.swing.JFrame;

import org.geotools.map.MapContent;
import org.geotools.ows.wmts.WebMapTileServer;
import org.geotools.ows.wmts.client.WMTSTileService;
import org.geotools.ows.wmts.model.TileMatrixSet;
import org.geotools.ows.wmts.model.WMTSCapabilities;
import org.geotools.ows.wmts.model.WMTSLayer;
import org.geotools.ows.wmts.model.WMTSServiceType;
import org.geotools.swing.JMapFrame;

/**
 * 天地图 经纬度 WGS84底图
 * 
 * 怀疑瓦片划区时错误，导致显示时进行了特殊的处理，待调查
 * 
 * @author zheng.liu
 *
 */
public class Class7WmtsLab extends JFrame {

  /**
   * 经纬度
   */
  private static final long serialVersionUID = 1L;

  /** 天地图-影像底图之经纬度-元数据地址 */
  static final String URL_IMG_C_CAPABILITIES = "http://t1.tianditu.gov.cn/img_c/wmts?request=GetCapabilities&service=wmts&tk=01ebabdef0269b945344a2a492a06b28";

  /** 天地图-影像底图之经纬度-瓦片数据地址 */
  static final String URL_IMG_C_TILES = "http://t1.tianditu.gov.cn/img_c/wmts?tk=01ebabdef0269b945344a2a492a06b28";

  /**
   * Prompts the user for a wms service, connects, and asks for a layer and then and displays its
   * contents on the screen in a map frame.
   */
  public static void main(String[] args) throws Exception {
    // 读取底图元数据的图层信息
    URL capabilitiesURL = new URL(URL_IMG_C_CAPABILITIES);
    WebMapTileServer wmts = new WebMapTileServer(capabilitiesURL);
    WMTSCapabilities capabilities = wmts.getCapabilities();
    List<WMTSLayer> layers = capabilities.getLayerList();

    // 读取经纬度底图的矩阵:c代表经纬度，参考天地图url
    TileMatrixSet tileMatrixSet = wmts.getCapabilities().getMatrixSet("c");

    // 面板内容定义
    MapContent mapcontent = new MapContent();
    mapcontent.setTitle("天地图 img_c/wmts");

    // 面板内容添加图层
    for (WMTSLayer wmtsLayer : layers) {
      WMTSTileService wmtsService = new WMTSTileService(
          URL_IMG_C_TILES + "&", // &后自动补充参数
          WMTSServiceType.KVP,
          wmtsLayer,
          "default",
          tileMatrixSet);
      Class7AsyncTileLayer myAsyncTileLayer = new Class7AsyncTileLayer(wmtsService);
      mapcontent.addLayer(myAsyncTileLayer);
    }

    // 显示面板内容
    // Now display the map
    JMapFrame frame = new JMapFrame(mapcontent);
    frame.setSize(256 * 9, 256 * 5);
    frame.enableStatusBar(true);
    frame.enableToolBar(true);
    frame.initComponents();
    frame.setVisible(true);
  }

}
